package pt.ipbeja.estig.eda;

import java.io.IOException;
import java.util.Random;

public class SortAlgorithms
{

   public static void main(String[] args)
   {
      int[] A = null;
      Random rnd = new Random();
      String[] opts = new String[] { "Bubble Sort", "Insertion Sort", "Heap Sort", "Merge Sort", "Quick Sort" };
      
      System.out.println("Qual o algoritmo que pretendo executar? ");
      System.out.println("1 - Bubble Sort");
      System.out.println("2 - Insertion Sort");
      System.out.println("3 - Heap Sort");
      System.out.println("4 - Merge Sort");
      System.out.println("5 - Quick Sort");
      
      
      int opt;
      try
      {
         opt = System.in.read() - 48;
         System.out.println("\n - " +  opts[ opt - 1 ] + " - " );
            
      for(int n = 50; n <= 1250; n+=50 )
      {
         A = new int[ n ];
         
         /**
          * Fill array with random numbers
          */
         for( int i = 0; i < n; i++ )
         {
            A[ i ] = rnd.nextInt( 1000 );
         }
         
         Object x = null;
         long media = 0;
         int kf = 1000;
         
         switch( opt )
         {
            case 1: 
               x = new BubbleSort( A );
               media = 0;
               
               for( int k = 0; k < kf; k++ )
               {
                  long t1 = System.nanoTime();
                  ((BubbleSort) x).sort();
                  long t2 = System.nanoTime();
                  
                  media += ( t2 - t1 );
               }
               
               media /= kf;
               System.out.println( "N: " + n + "\t" + String.format("%.10f", ( (media) * Math.pow(10, -9) ) ) );
               break;
               
            case 2:
               x = new InsertionSort( A );
               media = 0;
                kf = 1000;
               
               for( int k = 0; k < kf; k++ )
               {
                  long t1 = System.nanoTime();
                  ((InsertionSort) x).sort();
                  long t2 = System.nanoTime();
                  
                  media += ( t2 - t1 );
               }
               
               media /= kf;
               System.out.println( "N: " + n + "\t" + String.format("%.10f", ( (media) * Math.pow(10, -9) ) ) );
               break;
               
            case 3:
               x = new HeapSort( A );
               media = 0;
                kf = 1000;
               
               for( int k = 0; k < kf; k++ )
               {
                  long t1 = System.nanoTime();
                  ((HeapSort) x).sort();
                  long t2 = System.nanoTime();
                  
                  media += ( t2 - t1 );
               }
               
               media /= kf;
               System.out.println( "N: " + n + "\t" + String.format("%.10f", ( (media) * Math.pow(10, -9) ) ) );
               break;
               
            case 4:
               x = new MergeSort( A );
               media = 0;
                kf = 1000;
               
               for( int k = 0; k < kf; k++ )
               {
                  long t1 = System.nanoTime();
                  ((MergeSort) x).sort( 0, ((MergeSort) x).array.length - 1 );
                  long t2 = System.nanoTime();
                  
                  media += ( t2 - t1 );
               }
               
               media /= kf;
               System.out.println( "N: " + n + "\t" + String.format("%.10f", ( (media) * Math.pow(10, -9) ) ) );
               break;
            case 5:
               x = new QuickSort( A );
               media = 0;
               kf = 1000;
              
              for( int k = 0; k < kf; k++ )
              {
                 long t1 = System.nanoTime();
                 ((QuickSort) x).sort( 0, ((QuickSort) x).array.length - 1 );
                 long t2 = System.nanoTime();
                 
                 media += ( t2 - t1 );
              }
              
              media /= kf;
              System.out.println( "N: " + n + "\t" + String.format("%.10f", ( (media) * Math.pow(10, -9) ) ) );
              break;
         }
      }
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
   }
      
}